package com.ztgf.commodity.api.cms;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.commodity.models.bo.AttributesQueryBO;
import com.ztgf.commodity.models.service.ICommodityParamService;
import com.ztgf.commodity.models.vo.CommodityParamVO;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 商品参数信息表控制器
 *
 * @author chenyu.wang
 * @Date 2020-06-15 10:12:31
 */

@RestController
@RequestMapping("/cms/commodityParam")
@Slf4j
@Api(value = "CommodityParamController", tags = "商品参数信息表接口")
public class CommodityParamController {

    @DubboReference
    private ICommodityParamService commodityParamService;

    @ApiOperation(value = "添加商品参数信息表", notes = "添加商品参数信息表")
    @PostMapping(value = "/insert")
    public Result insert(String object) {
        if (!StringUtils.isNotBlank(object)) {
            return new Result<>(CodeEnum.FAIL_100002);
        } else {
            Result<List<String>> result = commodityParamService.newAdd(object);
            return new Result<>(result);
        }
    }

    @ApiOperation(value = "修改商品参数信息表", notes = "修改商品参数信息表")
    @PostMapping("/update")
    public Result update(String object) {
        if (!StringUtils.isNotBlank(object)) {
            return new Result<>(CodeEnum.FAIL_100002);
        } else {
            Result<List<String>> result = commodityParamService.newUpdate(object);
            return new Result<>(result);
        }
    }


    @ApiOperation(value = "删除/恢复商品参数信息表", notes = "删除/恢复商品参数信息表")
    @ApiImplicitParam(name = "id", value = "商品参数信息表id", required = true, paramType = "query", dataType = "Long")
    @PostMapping("/delete")
    public Result delete(Long id) {
        boolean flag = commodityParamService.updateState(id);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result get(Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        } else {
            CommodityParamVO vo = commodityParamService.getById(id);
            return new Result<>(vo);
        }
    }

    @PostMapping("/queryPageList")
    @ApiOperation(value = "商品参数信息表列表", notes = "商品参数信息表列表")
    public Result queryPageList(AttributesQueryBO bo) {
        Page<CommodityParamVO> list = commodityParamService.newQueryPageList(bo);
        return new Result<>(list);
    }

    @PostMapping("/newAdd")
    @ApiOperation(value = "新的添加参数属性接口", notes = "新的添加参数属性接口")
    public Result newAdd(String object) {
        if (!StringUtils.isNotBlank(object)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return new Result<>(commodityParamService.newAdd(object));
    }

    @PostMapping("/newUpdate")
    @ApiOperation(value = "新的修改参数属性接口", notes = "新的修改参数属性接口")
    public Result newUpdate(String object) {
        if (!StringUtils.isNotBlank(object)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return new Result<>(commodityParamService.newUpdate(object));
    }
}
